package com.itheima.reggie.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.itheima.reggie.common.BaseContext;
import com.itheima.reggie.common.R;
import com.itheima.reggie.entity.ShoppingCart;
import com.itheima.reggie.service.ShoppingCartService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@Slf4j
@RestController
@RequestMapping("/shoppingCart")
public class ShoppingCartController {

    @Resource
    private ShoppingCartService shoppingCartService;

    @GetMapping("/list")
    public R<List<ShoppingCart>> list(ShoppingCart shoppingCart){
        log.info("list:{}",shoppingCart);
        LambdaQueryWrapper<ShoppingCart> wrapper=new LambdaQueryWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        wrapper.orderByAsc(ShoppingCart::getCreateTime);
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }

    /**
     * 添加购物车
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/add")
    public R<ShoppingCart> add(@RequestBody ShoppingCart shoppingCart){
        log.info("list:{}",shoppingCart);
        // 设置用户id
        shoppingCart.setUserId(BaseContext.getCurrentId());
        //查询当前菜品或者套餐是否已存在
        Long dishId = shoppingCart.getDishId();
        LambdaUpdateWrapper<ShoppingCart> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());

        if(dishId!=null){
            //添加到购物车的是菜品
            wrapper.eq(ShoppingCart::getDishId, dishId);
        }else {
            //添加到购物车的是套餐
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart one = shoppingCartService.getOne(wrapper);
        if(one!=null){
            //如果存在就number+1
            one.setNumber(one.getNumber()+1);
            shoppingCartService.updateById(one);
            return R.success(one);
        }
        //如果不存在就添加
        shoppingCart.setNumber(1);
        shoppingCartService.save(shoppingCart);
        return R.success(shoppingCart);
    }

    /**
     * 减少购物车的单量
     *
     * @param shoppingCart
     * @return
     */
    @PostMapping("/sub")
    public R<List<ShoppingCart>> sub(@RequestBody ShoppingCart shoppingCart){
        log.info("list:{}",shoppingCart);
        Long dishId = shoppingCart.getDishId();
        LambdaUpdateWrapper<ShoppingCart> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        if(dishId!=null){
            //减少购物车中菜品的数量
            wrapper.eq(ShoppingCart::getDishId, dishId);
        }else {
            //减少购物车中套餐的数量
            wrapper.eq(ShoppingCart::getSetmealId, shoppingCart.getSetmealId());
        }
        ShoppingCart item = shoppingCartService.getOne(wrapper);
        if(item!=null){
            //如果存在就number-1
            item.setNumber(item.getNumber()-1);
            if(item.getNumber()<=0){
                shoppingCartService.removeById(item.getId());
            }
            shoppingCartService.updateById(item);
        }
        List<ShoppingCart> list = shoppingCartService.list(wrapper);
        return R.success(list);
    }


    /**
     * 清空购物车
     *
     * @param
     * @return
     */
    @DeleteMapping("/clean")
    public R<String> delete(){
       LambdaUpdateWrapper<ShoppingCart> wrapper=new LambdaUpdateWrapper<>();
        wrapper.eq(ShoppingCart::getUserId, BaseContext.getCurrentId());
        shoppingCartService.remove(wrapper);
        return R.success("清空购物车成功");
    }
}
